package com.yogee.topseller.fragment.my_order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ReturnContractActivity;
import com.yogee.topseller.activity.my.MyOrderActivity;
import com.yogee.topseller.activity.my.OrderDetailsActivity;
import com.yogee.topseller.activity.my.UploadVoucherActivity;
import com.yogee.topseller.adapter.ReservationAdapter;
import com.yogee.topseller.bean.MyOrderListBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/12 0012 11:00
 * 创建人：wangye
 * 修改备注：
 */
public class ProductReservationFragment extends HttpFragment implements ReservationAdapter.OnStatusClickListener, RefreshUtil.OnRefreshListener {
    @BindView(R.id.reservation_recycler)
    RecyclerView reservationRecycler;
    @BindView(R.id.reservation_tw)
    TwinklingRefreshLayout reservationTw;
    Unbinder unbinder;
    private int type;
    private ReservationAdapter adapter;
    private List<MyOrderListBean.ProductListBean> beans;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_product_reservation;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        type = getArguments().getInt("type");
        unbinder = ButterKnife.bind(this, super.onCreateView(inflater, container, savedInstanceState));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void initView() {
        reservationTw.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        reservationTw.setHeaderView(new RefreshView(getActivity()));
        reservationTw.setBottomView(new RefreshView(getActivity()));
        reservationTw.setBottomHeight(50);
        adapter = new ReservationAdapter(getActivity(), type);
        adapter.setOnStatusClickListener(this);
        reservationRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        reservationRecycler.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        myOrderList(AppUtil.getUserId(getActivity()), "0", "10", true);
    }

    @Override
    public void onCancleClick(String orderId) {
        cancelOrder(AppUtil.getUserId(getActivity()), orderId);
    }

    @Override
    public void onSaveClick(String orderId) {//上传凭证
        startActivity(new Intent(getActivity(), UploadVoucherActivity.class).putExtra("orderId",orderId));
    }

    @Override
    public void onReturnContract(String orderId) {//回寄合同
        startActivity(new Intent(getActivity(), ReturnContractActivity.class).putExtra("proId", orderId));
    }

    @Override
    public void onDetailsClick(String orderId) {
        startActivity(new Intent(getActivity(), OrderDetailsActivity.class).putExtra("proId",orderId));
    }

    private void myOrderList(String userId, String total, String count, final boolean isRefresh) {
        HttpManager.getInstance().myOrderList(userId, total, count, String.valueOf(type)).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<MyOrderListBean>() {
            @Override
            public void onNext(MyOrderListBean o) {
                if (isRefresh) {
                    beans = new ArrayList<>();
                    beans = o.getProductList();
                    reservationTw.finishRefreshing();
                } else {
                    beans.addAll(o.getProductList());
                    reservationTw.finishLoadmore();
                }
                adapter.addData(beans);
            }
        }, this));
    }
    
    private void cancelOrder(String userId, final String orderId) {
        HttpManager.getInstance().cancelOrder(userId, orderId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    for (int i = 0; i < beans.size(); i++) {
                        if (beans.get(i).getOrderId().equals(orderId)) {
                            beans.remove(i);
                        }
                    }
                }
                adapter.addData(beans);
                ((MyOrderActivity)getActivity()).getNum();
            }
        }, this));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        myOrderList(AppUtil.getUserId(getActivity()), "0", "10", true);
        ((MyOrderActivity)getActivity()).getNum();
    }

    @Override
    public void onLoad() {
        myOrderList(AppUtil.getUserId(getActivity()), String.valueOf(beans.size()), "10", false);
    }
}